package com.example.zsf.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zsf.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wd_mac on 2018/7/5.
 */

public class NavigationBar extends RelativeLayout {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.im_left)
    ImageView imLeft;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.line)
    View line;

    public NavigationBar(Context context) {
        super(context);
        initView(context);
    }

    public NavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(21)
    public NavigationBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.view_title, this));
    }

    public NavigationBar setTitleText(String string) {
        tvTitle.setText(string);
        return this;
    }

    public NavigationBar setRightText(String string) {
        tvRight.setText(string);
        return this;
    }

    public NavigationBar setRightImageResource(int imageResource) {
        ivRight.setImageResource(imageResource);
        return this;
    }

    public NavigationBar setTitleClickListener(OnClickListener listener) {
        tvTitle.setOnClickListener(listener);
        return this;
    }

    public NavigationBar setLeftWhite(){
        imLeft.setImageResource(R.mipmap.back);
        return this;
    }

    public NavigationBar setLeftClickListener(OnClickListener listener) {
        imLeft.setVisibility(VISIBLE);
        imLeft.setOnClickListener(listener);
        return this;
    }

    public NavigationBar setRightClickListener(OnClickListener listener) {
        if (!TextUtils.isEmpty(tvRight.getText())) {
            tvRight.setOnClickListener(listener);
            tvRight.setVisibility(View.VISIBLE);
        } else {
            ivRight.setOnClickListener(listener);
            ivRight.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public ImageView getIvRight() {
        return ivRight;
    }
}
